package com.cashbook.controller;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.junit.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.cashbook.pojo.TbInEx;
import com.cashbook.service.InExService;
import com.cashbook.utils.DateUtils;

import entity.PageResult;
import entity.Result;

/**
 * controller
 * 
 * @author Administrator
 *
 */
@RestController
@RequestMapping("/inex")
public class InExController {

	@Autowired
	private InExService inexService;
	

	/**
	 * 返回全部列表
	 * 
	 * @return
	 */
	@RequestMapping("/findAll")
	public List<TbInEx> findAll() {
		return inexService.findAll();
	}

	/**
	 * 返回全部列表
	 * 
	 * @return
	 */
	@RequestMapping("/findPage")
	public PageResult findPage(int page, int rows) {
		return inexService.findPage(page, rows);
	}

	/**
	 * 增加
	 * 
	 * @param article
	 * @return
	 */
	@RequestMapping("/add")
	public Result add(@RequestBody TbInEx inex) {
		try {
			
			inexService.add(inex);
			return new Result(true, "增加成功");
		} catch (Exception e) {
			e.printStackTrace();
			return new Result(false, "增加失败");
		}
	}

	/**
	 * 修改
	 * 
	 * @param article
	 * @return
	 */
	@RequestMapping("/update")
	public Result update(@RequestBody TbInEx inex) {
		try {
			inexService.update(inex);
			return new Result(true, "修改成功");
		} catch (Exception e) {
			e.printStackTrace();
			return new Result(false, "修改失败");
		}
	}

	/**
	 * 获取实体
	 * 
	 * @param id
	 * @return
	 */
	@RequestMapping("/findOne")
	public TbInEx findOne(Long id) {
		return inexService.findOne(id);
	}

	/**
	 * 批量删除
	 * 
	 * @param ids
	 * @return
	 */
	@RequestMapping("/delete")
	public Result delete(Long[] ids) {
		try {
			inexService.delete(ids);
			return new Result(true, "删除成功");
		} catch (Exception e) {
			e.printStackTrace();
			return new Result(false, "删除失败");
		}
	}

	/**
	 * 查询+分页
	 * 
	 * @param brand
	 * @param page
	 * @param rows
	 * @return
	 */
	@RequestMapping("/search")
	public PageResult search(@RequestBody TbInEx inex, int page, int rows) {
		return inexService.findPage(inex, page, rows);
	}
	/**查询明细
	 * 需要参数 Time cateId openid
	 * @param inex
	 * @return
	 * @throws ParseException 
	 */
	@RequestMapping("/searchByDetail")
	public List<TbInEx> searchByDetail(HttpServletRequest request,HttpServletResponse response) {
		String time=request.getParameter("time");//日期 2018-01-02
		Long   cateId= null;
		try {
				cateId=Long.parseLong(request.getParameter("cateId"));
			}catch(Exception e) {
			}
		
		String openId=request.getParameter("openId") ;
		return inexService.searchByDetail(time,cateId,openId);
		
	}
	/**查询账单
	 * 需要参数 cate_id 0/1,open_Id,time 2018
	 * @param inex
	 * @return
	 */
	@RequestMapping("/selectBytypeName")
	public List<Map<String,Object>> selectBytypeName(HttpServletRequest request,HttpServletResponse response){
		SimpleDateFormat sm = new SimpleDateFormat("yyyy-MM-dd");
		TbInEx inex=new TbInEx();
		Long cateId=null;
		if(request.getParameter("cateId")!=null) {
			cateId=Long.parseLong(request.getParameter("cateId"));
			inex.setCateId(cateId);

		}
		inex.setOpenId(request.getParameter("openId"));
		try {
			inex.setTime(sm.parse(request.getParameter("time")));
		} catch (ParseException e) {
		}
		List<Map<String,Object>> list=inexService.selectBytypeName(inex);
		return list;
		
	}
	/**
	 * 按月查询 echart图
	 * @param request openid cateid time
	 * @param response 
	 * @return
	 */
	@RequestMapping("/selectEchartByMonth")
	public List<Map<String,Object>> selectEchartByMonth(HttpServletRequest request ,HttpServletResponse response){
		SimpleDateFormat sm = new SimpleDateFormat("yyyy-MM-dd");
		TbInEx inex=new TbInEx();
		Long cateId=null;
		String openId=null;
		String time=null;

		if(request.getParameter("cateId")!=null) {
			inex.setCateId(Long.parseLong(request.getParameter("cateId")));
			cateId=Long.parseLong(request.getParameter("cateId"));

		}
		if(request.getParameter("openId")!=null) {
			inex.setOpenId(request.getParameter("openId"));
			openId=request.getParameter("openId");
		}
		if(request.getParameter("time")!=null) {
			try {
				inex.setTime(sm.parse(request.getParameter("time")));
				time=request.getParameter("time");
			} catch (ParseException e) {
			}
		}
		
		List<Map<String,Object>> list=inexService.selectEchartByMonth(inex);
		return list;
		
	}
	/**
	 * 
	 * @param request openId cateid typeWeek:0 代表本周 1代表上周
	 * @param response
	 * @return
	 */
	@RequestMapping("/selectEchartByWeek")
	public List<Map<String,Object>> selectEchartByWeek(HttpServletRequest request,HttpServletResponse response){
		String openId=request.getParameter("openId");
		Long   cateId= null;
		if(request.getParameter("cateId")!=null) {
			cateId=Long.parseLong(request.getParameter("cateId"));
		 }
		String typeWeek=request.getParameter("typeWeek");
		SimpleDateFormat sdf=new SimpleDateFormat("yyyy-MM-dd");
		Date beginTime=null;
		Date endTime=null;
		DateUtils du=new DateUtils();
		if(typeWeek.equals("0")) {
				beginTime=du.getBeginDayOfWeek();//本周开始日期
				endTime=du.getEndDayOfWeek();//本周结束日期
		}else if(typeWeek.equals("1")) {
			beginTime=du.getFrontDay(du.getBeginDayOfWeek(), 7);//上周开始日期
			endTime=du.getFrontDay(du.getEndDayOfWeek(),7);//上周结束日期
		}
		return inexService.selectEchartByWeek(openId, beginTime, endTime, cateId);
		
	}
	@RequestMapping("/selectAll")
	public PageResult selectAll( int page, int rows,@RequestBody TbInEx inex){
		return inexService.selectAll(page,rows,inex);
		
	}
	
}
